package com.example.jnnews;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.google.android.material.switchmaterial.SwitchMaterial;
import java.util.ArrayList;
import java.util.List;

public class SettingsActivity extends AppCompatActivity {

    private SwitchMaterial switchAdmin;
    private RecyclerView recyclerSettings;
    private SharedPreferences prefs;
    private SettingsAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_settings);

        // 初始化视图
        switchAdmin = findViewById(R.id.switch_admin);
        recyclerSettings = findViewById(R.id.recycler_settings);
        prefs = getSharedPreferences("jnnews_prefs", MODE_PRIVATE);

        // 配置RecyclerView
        recyclerSettings.setLayoutManager(new LinearLayoutManager(this));
        adapter = new SettingsAdapter();
        recyclerSettings.setAdapter(adapter);

        // 初始化开关状态
        boolean isAdmin = prefs.getBoolean("admin_mode", false);
        switchAdmin.setChecked(isAdmin);
        refreshSettings();

        // 设置监听器
        switchAdmin.setOnCheckedChangeListener((buttonView, isChecked) -> {
            prefs.edit().putBoolean("admin_mode", isChecked).apply();
            refreshSettings();
        });
    }
    private void refreshSettings() {
        List<SettingItem> items = new ArrayList<>();

        // 固定项目
        items.add(new SettingItem(
                R.string.account_management,
                v -> startActivity(new Intent(this, AccountManagementActivity.class))
        ));

        // 管理员功能
        if (switchAdmin.isChecked()) {
            items.add(new SettingItem(
                    R.string.add_article,
                    v -> startActivity(new Intent(this, AddArticleActivity.class))
            ));
            items.add(new SettingItem(
                    R.string.add_product,
                    v -> startActivity(new Intent(this, AddProductActivity.class))
            ));
        }

        // 退出登录
        items.add(new SettingItem(
                R.string.logout,
                v -> {
                    prefs.edit().remove("currentUser").apply();
                    startActivity(new Intent(this, LoginActivity.class));
                    finishAffinity();
                })
        );

        adapter.submitList(items);
    }

    // RecyclerView适配器
    private class SettingsAdapter extends RecyclerView.Adapter<SettingsAdapter.ViewHolder> {

        private List<SettingItem> items = new ArrayList<>();

        public void submitList(List<SettingItem> newItems) {
            items.clear();
            items.addAll(newItems);
            notifyDataSetChanged();
        }

        @NonNull
        @Override
        public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.item_setting, parent, false);
            return new ViewHolder(view);
        }

        @Override
        public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
            SettingItem item = items.get(position);
            holder.tvSetting.setText(item.textRes);
            holder.itemView.setOnClickListener(item.listener);
        }

        @Override
        public int getItemCount() {
            return items.size();
        }

        class ViewHolder extends RecyclerView.ViewHolder {
            TextView tvSetting;

            ViewHolder(View itemView) {
                super(itemView);
                tvSetting = itemView.findViewById(R.id.tv_setting);
            }
        }
    }

    // 设置项数据类
    private static class SettingItem {
        int textRes;
        View.OnClickListener listener;

        SettingItem(int textRes, View.OnClickListener listener) {
            this.textRes = textRes;
            this.listener = listener;
        }
    }
}